package com.core.life.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.core.common.dao.BaseDaoImpl;
import com.core.common.dao.Pager;
import com.core.life.dao.IConsumeRecordDao;
import com.core.life.entity.ConsumeRecordEntity;

@Repository
public class ConsumeRecordDaoImpl extends BaseDaoImpl<ConsumeRecordEntity>
implements IConsumeRecordDao{

	@Override
	public void deleteByIds(List<Long> ids) {
		String hql = "update ConsumeRecordEntity cr set cr.deleteFlag=true where cr.id in (:ids)";
		this.getSession().createQuery(hql).setParameterList("ids", ids).executeUpdate();
	}

	@Override
	public void deleteByConsumeItemIds(List<Long> itemIds) {
		String hql = "update ConsumeRecordEntity cr set cr.deleteFlag=true where cr.consumeItem.id in (:ids)";
		this.getSession().createQuery(hql).setParameterList("ids", itemIds).executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Pager<ConsumeRecordEntity> selectByOption(int pageIndex, int pageSize, 
			String sort, String order,Map<String, Object> optionMap) {
		String hql = "from ConsumeRecordEntity cr where cr.deleteFlag=false and cr.year=:year and cr.month=:month";
		String countHql = "select count(*) from ConsumeRecordEntity cr where cr.deleteFlag=false and cr.year=:year and cr.month=:month";
		if(StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)){
			hql += " order by cr." + sort + " " + order;
		}else{
			hql += " order by cr.createDate desc";
		}
		Query query = this.getSession().createQuery(hql);
		Query countQuery = this.getSession().createQuery(countHql);
		query.setParameter("year", optionMap.get("year")).setParameter("month", optionMap.get("month"));
		countQuery.setParameter("year", optionMap.get("year")).setParameter("month", optionMap.get("month"));
		Long totalCount = 0l;
		Object obj = countQuery.uniqueResult();
		if(obj != null){
			totalCount = (Long) obj;
		}
		//设置每页显示多少个，设置最大结果
		query.setMaxResults(pageSize);
		//设置起点
		query.setFirstResult(pageSize * (pageIndex - 1));
		return new Pager<ConsumeRecordEntity>(pageIndex, pageSize, totalCount, query.list());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ConsumeRecordEntity> selectByYearAndMonth(Integer year, Integer month){
		String hql = "from ConsumeRecordEntity cr where cr.deleteFlag=false and cr.year =? and cr.month =?";
		return this.getSession().createQuery(hql).setParameter(0, year).setParameter(1, month).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ConsumeRecordEntity> selectByYearAndMonthAndConsumeUserIds(Integer year, Integer month,
			String consumeUserIds) {
		String hql = "from ConsumeRecordEntity cr where cr.deleteFlag=false and cr.year =? and cr.month =? and cr.consumeUserIds=?";
		return this.getSession().createQuery(hql).setParameter(0, year).setParameter(1, month).setParameter(2, consumeUserIds).list();
	}
	
	@SuppressWarnings("unchecked")
    @Override
    public List<Long> selectIdsByYearAndMonthAndConsumeUserIds(Integer year, Integer month,
            String consumeUserIds) {
      String hql = "select cr.id from ConsumeRecordEntity cr where cr.deleteFlag=false and cr.year =? and cr.month =? and cr.consumeUserIds=?";
        return this.getSession().createQuery(hql).setParameter(0, year).setParameter(1, month).setParameter(2, consumeUserIds).list();
    }
	
	@SuppressWarnings("unchecked")
    @Override
    public List<Object[]> selectSumByIds(List<Long> ids) {
        String sql = "SELECT cr.payerUserId,SUM(cr.consumeMoney) from t_consume_record cr "
                + "where cr.id in (:ids) GROUP BY cr.payerUserId";
        return this.getSession().createSQLQuery(sql).setParameterList("ids", ids).list();
    }

}
